/* 
 * File:   main.c
 * Author: bjorn
 *
 * Created on September 2, 2007, 10:53 AM
 */

#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include "screen.h"
#include "tilemap.h"
#include "resource.h"
#include "rope.h"
#include "script.h"
#include "joystick.h"
#include "input.h"

int eruta_init() {
    int ok;
    ok = SDL_Init(SDL_INIT_EVERYTHING);
    if(!ok) return -1; 
	if(TTF_Init()==-1) {
    	printf("TTF_Init: %s\n", TTF_GetError());
    	exit(2);
	}
    return 0;
}

int eruta_done() {
    SDL_Quit();
	TTF_Quit();
    return 0;
}


int test_screen(Resource & resource) {
    Image tile1, tile2;
	int xindex, ind1, ind2, before, after;
	int yindex     ;
	Tilemap *tilemap = new Tilemap();
	Screen screen;
	Image ok = screen.set(640, 480, 16, true);
    if(!ok) return -1;
	/*
	Joysticklist jslist = Joystick::open_all();
	if(jslist.size() > 0) {
		Joystick::enable_events();
	}
	for(int index = 0; index < jslist.size(); index++) {
		Joystickpointer joy = jslist[index]	;
		if(!joy) continue;
		printf("Joystick detected: %s, axes: %d, buttons:  %d \n",
		 Joystick::name(index),
		Joystick::axe_count(joy),
		Joystick::button_count(joy)
		);
	}
	*/
    tile1 = resource.loadimage_display_c("tile/tile_floor_grass.png");
	// screen.make_image(32, 32);
    tile2 = resource.loadimage_display_c("tile/tile_water_light_a1.png");
	// screen.make_image(32, 32);
    Image tile3 = resource.loadimage_display_c("tile/tile_water_light_a2.png");

	// = screen.make_image(32, 32);
    /*
    image_fill_all(tile1, 0, 140, 0);
	image_fill_all(tile2, 0, 120, 0);
    image_fill_all(tile3,   0, 255, 0);
	*/	

	Maptile mtile1;
	mtile1.image_add(tile1);
	Maptile mtile2;
	mtile2.image_add(tile2);
	mtile2.image_add(tile3);

	Maplayer layer0(100, 100);;
	tilemap->layers[0] = layer0;
    ind1  = tilemap->layers[0].add(&mtile1);
    ind2  = tilemap->layers[0].add(&mtile2);
    image_fill_all(screen._surface, 0, 150, 0);    

    for(xindex = 0; xindex < 100; xindex ++)    {
        for(yindex = 0; yindex < 100; yindex ++)    {
            tilemap->layers[0].set(xindex, yindex, ind1);
            if(xindex == yindex) { 
                tilemap->layers[0].set(xindex, yindex, ind2 );
            }
        }
    }

	
	Image entity_i1 = screen.make_image(40, 80);
	Mapentity entity(MAPENTITY_PLAYER, 100, 100);
	entity.image = entity_i1;
	tilemap->entities[0].push_back(&entity);
	tilemap->layer_count = 1;
	

	if(TTF_Init()==-1) {
    	printf("TTF_Init: %s\n", TTF_GetError());
	}
    
	Fontpointer font = resource.loadfont_c("crystal.ttf");
	if(!font) {
		printf("Could not load font: %s\n", TTF_GetError() );
		return 0;
	}
	
	
	Input input(true);
	for(int index = 0; index < input.joysticks.size(); index++) {
		Joystickpointer joy = input.joysticks[index];
		if(!joy) continue;
		printf("Joystick detected: %s, axes: %d, buttons:  %d \n",
			Joystick::name(index),
			Joystick::axe_count(joy),
			Joystick::button_count(joy)
		);
	}
    before = SDL_GetTicks();	
	int finished = 0;	
	int frames   = 0;
	while(!finished) {
		input.process();
		if(input.is_quit()) { finished = 1; break; }
		if(input.is_up()) { entity.speed.vy = -1; } 
		else if(input.is_down()) { entity.speed.vy = 1; } 
		else { entity.speed.vy = 0; } 
		if(input.is_left()) { entity.speed.vx = -1; } 
		else if(input.is_right()) { entity.speed.vx = 1; } 
		else { entity.speed.vx = 0; } 
		tilemap->entities_update();

		//input.state_show();
		/*Image rendered = Font::draw_multi(screen, font, 
	"This is a long story that will span many lines in many books. It is a story of tomorrow, but perhaps this tomorrow will become today one day. It is the story of the newest city of mankind. \n\n\nEruta. ", 200);
		// layer.draw(screen, xindex, xindex); 		
		*/
		tilemap->draw(screen);
		/* if(rendered) screen.blit(xindex / 100, xindex / 100, rendered); */
		frames ++;
        /*
		
        layer.draw(screen, xindex, xindex); 
        layer.draw(screen, xindex, xindex); 
        layer.draw(screen, xindex, xindex); 
		*/
        screen.flip();
		// image_free(rendered);
    }
	
	// delete layer;
    after = SDL_GetTicks();
    printf("%d renders in %d time: %f FPS\n", frames, after - before, 
		( (double)frames * 1000.0 /(double)(after-before)));
	//Joystick::close_all(jslist);	
	delete tilemap;
    return 0;
}


int test() {
    Rope getstr;
	Resource resource;
	Lua script;
	script.eval_c("a={}");
	script.eval_c("a.a=\"Lua Lua Lua!\"");
    script.eval_c("print(a.a)");
    int ok = resource.scan();
	if(!ok) {
		printf("Could not find resource directory!");
	} else {
		Image angtil2;	
		getstr = resource._script.getstring("ERUTA_TEST", "default");
		printf("Got rope from LUA: %s\n", getstr.c_str()); 
		angtil2 = resource.loadimage_c("angband_tiles_32x32.png");
		if(!angtil2) printf("Image 2 not loaded!");
    	else { printf("Image 2 OK\n");  image_free(angtil2);  }	
	}





/*	
    Script script = script_make();
	Rope s1 = rope_make("Hello ");
    Rope s2 = rope_make("World!");
	Image angtil = image_load_c("share/image/angband_tiles_32x32.png");
	script_eval_c(script, "a={}");
	script_eval_c(script, "a.a=\"Lua Lua Lua!\"");
    script_eval_c(script, "print(a.a)");
    getstr = script_get_rope(script, "a.a", "Default Value");
    printf("Got string from LUA: %s\n", rope_c(getstr)); 
*/	
    /*
	* printf("Resource dir %s exists: %d\n", RESOURCE_DIRECTORY, file_exists(RESOURCE_DIRECTORY));
	*/
    /*
	printf("Rope %s %s\n", rope_c(s1) , rope_c(s2));
	rope_free(getstr);    
    rope_free(s2);
    rope_free(s1); 
    
    if(!angtil) printf("Image not loaded!");
    else { printf("Image OK\n");  image_free(angtil);  }
	
	script_free(script); 
	*/
    return 0;
}

/*
 * 
 */
int main(int argc, char* argv[]) {
    
    printf("Starting eruta... ");
    if(!eruta_init()) { printf("init FAILED!\n"); return -1; }
    printf("OK!\n");
    Resource resource;
    int ok = resource.scan();
	if(!ok) {
		printf("Could not find resource directory!");
		return 1;
	} 

    test_screen(resource);
    /*

    */
    /*
    Vector matr  = vector_make(sizeof(Vector), 100);
    size_t stop = vector_size(matr);
    for(size_t index = 0; index < stop; index ++ ) {
         vector_set(matr, index, vector_make(sizeof(int), 100));            
    }

    Vector vect  = vector_make(sizeof(char *), 100);
    vector_set(vect, 22, "Hello");
    vector_set(vect, 33, "World");
    
    Vector row = vector_get(matr, 10);
    vector_set(row, 10, 69);
    
    printf("%d %d; %d\n", screen_wide(screen), screen_high(screen), screen_tester_get(screen));
    printf("%s %s \n", vector_get(vect, 22), vector_get(vect, 33));
    
    vector_each(matr, (vector_each_callback) &vector_wipe);
    vector_free(matr);
    */

    
    eruta_done(); 
    
    return (EXIT_SUCCESS);
}

